//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;

namespace CommunityServer.Components
{
	/// <summary>
	/// Manages views by PostID
	/// </summary>
	public class Views
	{
        private static Hashtable viewList = new Hashtable();

		private Views()
		{
			
		}

        /// <summary>
        /// Adds or incriments the AggView count for the PostID
        /// </summary>
        public static void AddAggCount(int PostID)
        {
            lock(viewList.SyncRoot)
            {
                View v = viewList[PostID] as View;
                if(v == null)
                {
                    v = new View(PostID);
                    viewList.Add(PostID,v);
                }
                v.AggCount ++;
            }
        }

        /// <summary>
        /// Adds or incriments the WebView count for the PostID
        /// </summary>
        public static void AddWebCount(int PostID)
        {
            lock(viewList.SyncRoot)
            {
                View v = viewList[PostID] as View;
                if(v == null)
                {
                    v = new View(PostID);
                    viewList.Add(PostID,v);
                }
                v.WebCount ++;
            }
        }

        /// <summary>
        /// Creates a copy of the internal hashtable, clears the internal hashtable and returns the 
        /// copied hashtable
        /// </summary>
        public static Hashtable ReQueue()
        {
            Hashtable views = null;
            lock(viewList.SyncRoot)
            {
                views = new Hashtable(viewList);
                viewList.Clear();
            }
            return views;
        }

        /// <summary>
        /// Saves the current views to the datastore. (Uses ReQueue())
        /// </summary>
        public static void SaveQueue()
        {
            Hashtable views = ReQueue();
            CommonDataProvider dp = CommonDataProvider.Instance();
            dp.SaveViewList(views);
        }
	}
}
